package com.tony.mindhr.controller.system.basic;

import com.tony.mindhr.model.Department;
import com.tony.mindhr.model.RespBean;
import com.tony.mindhr.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: DepartmentController
 * @Auther: 皮蛋布丁
 * @Date: 2021/06/05/16:23
 * @Description: 部门管理
 */
@RestController
@RequestMapping("/system/basic/department")
public class DepartmentController {

    @Autowired
    DepartmentService departmentService;

    /**
    * @Description: getAllDepartments 获取所有部门(使用递归/懒加载的形式)
    * @Param: []
    * @return: java.util.List<com.tony.mindhr.model.Department>
    * @Author: 皮蛋布丁
    * @Date: 2021/6/5 16:27
    */
    @GetMapping("/")
    public List<Department> getAllDepartments() {
        return departmentService.getAllDepartments();
    }

    /**
    * @Description: addDep 添加部门
    * @Param: [dep]
    * @return: com.tony.mindhr.model.RespBean
    * @Author: 皮蛋布丁
    * @Date: 2021/6/6 16:42
    */
    @PostMapping("/")
    public RespBean addDep(@RequestBody Department dep) {
        departmentService.addDep(dep);
        if (dep.getResult() == 1) {
            return RespBean.ok("添加成功!",dep);
        }
        return RespBean.error("添加失败！");
    }

    /**
    * @Description: deleteDepById 根据id删除部门
    * @Param: [id]
    * @return: com.tony.mindhr.model.RespBean
    * @Author: 皮蛋布丁
    * @Date: 2021/6/6 21:20
    */
    @DeleteMapping("/{id}")
    public RespBean deleteDepById(@PathVariable Integer id) {
        Department dep = new Department();
        dep.setId(id);
        departmentService.deleteDepById(dep);
        if (dep.getResult() == -2) {
            return RespBean.error("该部门下存在子部门，删除失败！");
        } else if (dep.getResult() == -1) {
            return RespBean.error("该部门下存在员工，删除失败！");
        } else if (dep.getResult() == 1) {
            return RespBean.ok("删除成功！");
        }
        return RespBean.error("删除失败!");
    }







}
